package com.cyandpos.pad.codedcoupon;

import android.text.TextUtils;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.CodedCouponData;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.google.gson.JsonObject;

/**
 * Created by Administrator on 2020/4/16.
 */

public class CodedCouponModelImpl implements ICodedCouponModel{

    /**
     * 编码券查询
     * @param reqData
     * @param listener
     */
    @Override
    public void getCodedCouponList(final QueryMemberRequest reqData, final queryCodedCouponListener listener) {
        PosHttpManager.getCodedCouponList(reqData, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {
                BaseResponse<CodedCouponData> baseResponse = BaseResponse.fromJson(res.toString(),CodedCouponData.class);
                if(baseResponse.getCode().equals("0")){
                    if(baseResponse.getTips()!=null && baseResponse.getTips().length()>0){
                        listener.getCodedCouponFailure(baseResponse.getTips());
                    }else{
                        try {
                            if(baseResponse.getData()==null){
                                listener.getCodedCouponFailure("无可用券信息！");
                            }else{
                                listener.getCodedCouponSuccess(baseResponse.getData(),reqData.getCodedCouponType());
                            }
                        }catch (Exception e){
                            listener.getCodedCouponFailure("客户端处理异常："+ e.getMessage());
                        }
                    }

                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.getCodedCouponFailure(baseResponse.getMsg());
                    }else {
                        listener.getCodedCouponFailure(baseResponse.getTips());
                    }
                }


//                String retCode = PosUtils.joAsString(response,"retCode");
//                String retMessage = PosUtils.joAsString(response,"retMessage");
//                ActionLog.recordActionInfoLog("codeCoupon--服务端返回的json串："+ response);
//                if(retCode.equals("00")){
//                    JsonObject json = response.getAsJsonObject("data");
//                    CodedCouponData data = PosUtils.GSON.fromJson(json , CodedCouponData.class);
//                    if(reqData.getCodedCouponType() == 0){//0 券号查询
//                        mPresenter.getDataSuccess(data ,0);
//                    }else{//刷卡查询
//                        mPresenter.getDataSuccess(data ,1);
//                    }
//                } else {
//                    mPresenter.getDataFailure(retMessage);
//                }


            }

            @Override
            public void onFailure(String msg) {
                listener.getCodedCouponFailure(msg);
            }
        });

    }
}
